package DTO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import util.DBUtil;

/**
 * <p>Java class for Project complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="Project">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="projectID" type="{http://www.w3.org/2001/XMLSchema}int"/>
 *         &lt;element name="projectManager" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="projectName" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="description" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="numOfMinestone" type="{http://www.w3.org/2001/XMLSchema}int"/>
 *         &lt;element name="startDay" type="{http://xml.netbeans.org/schema/ProjectManagement}Date"/>
 *         &lt;element name="endDay" type="{http://xml.netbeans.org/schema/ProjectManagement}Date"/>
 *         &lt;element name="status" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Project", propOrder = {
    "projectID",
    "projectManager",
    "projectName",
    "description",
    "numOfMinestone",
    "startDay",
    "endDay",
    "status"
})
public class Project {

    protected int projectID;
    @XmlElement(required = true)
    protected String projectManager;
    @XmlElement(required = true)
    protected String projectName;
    @XmlElement(required = true)
    protected String description;
    protected int numOfMinestone;
    @XmlElement(required = true)
    protected Date startDay;
    @XmlElement(required = true)
    protected Date endDay;
    @XmlElement(required = true)
    protected String status;

    public Project() {
        startDay = new Date();
        endDay = new Date();
    }

    public Project(int projectID, String projectManager, String projectName, String description, int numOfMinestone, Date startDay, Date endDay, String status) {
        this.projectID = projectID;
        this.projectManager = projectManager;
        this.projectName = projectName;
        this.description = description;
        this.numOfMinestone = numOfMinestone;
        this.startDay = startDay;
        this.endDay = endDay;
        this.status = status;
    }

    public Project(String projectName, String projectManager, String description, int numOfMinestone, Date startDay, Date endDay, String status) {
        this.projectName = projectName;
        this.projectManager = projectManager;
        this.description = description;
        this.numOfMinestone = numOfMinestone;
        this.startDay = startDay;
        this.endDay = endDay;
        this.status = status;
    }

    public Project(int parseInt) {
        this.projectID = parseInt;
        startDay = new Date();
        endDay = new Date();
    }

    /**
     * Gets the value of the projectID property.
     * 
     */
    public int getProjectID() {
        return projectID;
    }

    /**
     * Sets the value of the projectID property.
     * 
     */
    public void setProjectID(int value) {
        this.projectID = value;
    }

    /**
     * Gets the value of the projectManager property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getProjectManager() {
        return projectManager;
    }

    /**
     * Sets the value of the projectManager property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setProjectManager(String value) {
        this.projectManager = value;
    }

    /**
     * Gets the value of the projectName property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getProjectName() {
        return projectName;
    }

    /**
     * Sets the value of the projectName property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setProjectName(String value) {
        this.projectName = value;
    }

    /**
     * Gets the value of the description property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getDescription() {
        return description;
    }

    /**
     * Sets the value of the description property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setDescription(String value) {
        this.description = value;
    }

    /**
     * Gets the value of the numOfMinestone property.
     * 
     */
    public int getNumOfMinestone() {
        return numOfMinestone;
    }

    /**
     * Sets the value of the numOfMinestone property.
     * 
     */
    public void setNumOfMinestone(int value) {
        this.numOfMinestone = value;
    }

    /**
     * Gets the value of the startDay property.
     * 
     * @return
     *     possible object is
     *     {@link Date }
     *     
     */
    public Date getStartDay() {
        return startDay;
    }

    /**
     * Sets the value of the startDay property.
     * 
     * @param value
     *     allowed object is
     *     {@link Date }
     *     
     */
    public void setStartDay(Date value) {
        this.startDay = value;
    }

    /**
     * Gets the value of the endDay property.
     * 
     * @return
     *     possible object is
     *     {@link Date }
     *     
     */
    public Date getEndDay() {
        return endDay;
    }

    /**
     * Sets the value of the endDay property.
     * 
     * @param value
     *     allowed object is
     *     {@link Date }
     *     
     */
    public void setEndDay(Date value) {
        this.endDay = value;
    }

    /**
     * Gets the value of the status property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getStatus() {
        return status;
    }

    /**
     * Sets the value of the status property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setStatus(String value) {
        this.status = value;
    }

    public boolean UpdateProject() {

        DBUtil util = new DBUtil();
        Connection conn = null;
        PreparedStatement stm = null;
        try {
            Class.forName(util.getDriverName());
            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
            conn = DriverManager.getConnection(util.getURL(), util.getUsername(), util.getPassword());
            if (conn != null) {

                String sql = "UPDATE Projects SET [Description] = ?,[StartDay] = ?,[EndDay] = ?,[Status] = ? WHERE ProjectID = ?";
                stm = conn.prepareStatement(sql);
                stm.setString(1, this.getDescription());
                stm.setDate(2, this.getStartDay().getDateJavaUtil());
                stm.setDate(3, this.getEndDay().getDateJavaUtil());
                stm.setString(4, this.getStatus());
                stm.setInt(5, this.getProjectID());

                int row = stm.executeUpdate();
                if (row > 0) {
                    //lay return key
                    //for i 1 to numofproject
                    //tao minestone;
                    //insert minestone ben ham minestone viet insert minestone
                    return true;
                }
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Project.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
                if (stm != null) {
                    stm.close();
                }
                
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public boolean saveProject() {
        Connection con = null;
        PreparedStatement stm = null;
        DBUtil dbUtil = new DBUtil();
        try {

            int returnID = -1;
            Class.forName(dbUtil.getDriverName());
            con = DriverManager.getConnection(dbUtil.getURL(), dbUtil.getUsername(), dbUtil.getPassword());
            String sql = "Insert into Projects Values (?,?,?,?,?,?,?)";
            stm = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            stm.setString(1, this.projectName);
            stm.setString(2, this.projectManager);
            stm.setString(3, this.description);
            stm.setInt(4, this.numOfMinestone);
            stm.setDate(5, this.startDay.getDateJavaUtil());
            stm.setDate(6, this.endDay.getDateJavaUtil());
            stm.setString(7, this.status);
            int row = stm.executeUpdate();

            ResultSet rs = stm.getGeneratedKeys();
            if (rs.next()) {
                returnID = rs.getInt(1);
            }

            if (returnID != -1) {
                this.setProjectID(returnID);
                Minestone minestone = new Minestone();
                minestone.setProjectID(this.getProjectID());
                minestone.setStartDay(this.getStartDay());
                minestone.setEndDay(this.getEndDay());
                minestone.setStatus(this.getStatus());
                for (int i = 1; i <= this.getNumOfMinestone(); i++) {
                    minestone.InsertMinestone(i);
                }
            }
            if (row > 0) {

                return true;
            }
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return false;
        }
    }
}
